<template>
  <div>
    <el-form
      class="detail-form-content"
      ref="ruleForm"
      :model="ruleForm"
      label-width="80px"
    >  
     <el-row>
                    <el-col :span="12">
           <el-form-item v-if="flag=='jiaoshi'"  label='教师姓名' prop="jiaoshiName">
               <el-input v-model="ruleForm.jiaoshiName"  placeholder='教师姓名' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiaoshi'"  label='教师手机号' prop="jiaoshiPhone">
               <el-input v-model="ruleForm.jiaoshiPhone"  placeholder='教师手机号' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiaoshi'"  label='教师身份证号' prop="jiaoshiIdNumber">
               <el-input v-model="ruleForm.jiaoshiIdNumber"  placeholder='教师身份证号' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
             <el-form-item v-if="flag=='jiaoshi'" label='教师头像' prop="jiaoshiPhoto">
                 <file-upload
                         tip="点击上传照片"
                         action="file/upload"
                         :limit="3"
                         :multiple="true"
                         :fileUrls="ruleForm.jiaoshiPhoto?ruleForm.jiaoshiPhoto:''"
                         @change="jiaoshiPhotoUploadChange"
                 ></file-upload>
             </el-form-item>
         </el-col>
         <el-col :span="12">
             <el-form-item v-if="flag=='jiaoshi'"  label='科目' prop="kemuTypes">
                 <el-select v-model="ruleForm.kemuTypes"  placeholder='请选择科目'>
                     <el-option
                             v-for="(item,index) in kemuTypesOptions"
                             v-bind:key="item.codeIndex"
                             :label="item.indexName"
                             :value="item.codeIndex">
                     </el-option>
                 </el-select>
             </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item v-if="flag=='jiaoshi'"  label='电子邮箱' prop="jiaoshiEmail">
               <el-input v-model="ruleForm.jiaoshiEmail"  placeholder='电子邮箱' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='家长姓名' prop="jiazhangxueshengJiazhangname">
               <el-input v-model="ruleForm.jiazhangxueshengJiazhangname"  placeholder='家长姓名' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='家长联系方式' prop="jiazhangxueshengPhone">
               <el-input v-model="ruleForm.jiazhangxueshengPhone"  placeholder='家长联系方式' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='学号' prop="jiazhangxueshengUuidNumber">
               <el-input v-model="ruleForm.jiazhangxueshengUuidNumber"  placeholder='学号' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='学生姓名' prop="jiazhangxueshengXuesheng">
               <el-input v-model="ruleForm.jiazhangxueshengXuesheng"  placeholder='学生姓名' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='学生身份证号' prop="jiazhangxueshengIdNumber">
               <el-input v-model="ruleForm.jiazhangxueshengIdNumber"  placeholder='学生身份证号' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
             <el-form-item v-if="flag=='jiazhangxuesheng'" label='学生头像' prop="jiazhangxueshengPhoto">
                 <file-upload
                         tip="点击上传照片"
                         action="file/upload"
                         :limit="3"
                         :multiple="true"
                         :fileUrls="ruleForm.jiazhangxueshengPhoto?ruleForm.jiazhangxueshengPhoto:''"
                         @change="jiazhangxueshengPhotoUploadChange"
                 ></file-upload>
             </el-form-item>
         </el-col>
         <el-col :span="12">
             <el-form-item v-if="flag=='jiazhangxuesheng'"  label='班级' prop="banjiTypes">
                 <el-select v-model="ruleForm.banjiTypes"  placeholder='请选择班级'>
                     <el-option
                             v-for="(item,index) in banjiTypesOptions"
                             v-bind:key="item.codeIndex"
                             :label="item.indexName"
                             :value="item.codeIndex">
                     </el-option>
                 </el-select>
             </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='年级' prop="jiazhangxueshengNianji">
               <el-input v-model="ruleForm.jiazhangxueshengNianji"  placeholder='年级' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
           <el-form-item v-if="flag=='jiazhangxuesheng'"  label='联系邮箱' prop="jiazhangxueshengEmail">
               <el-input v-model="ruleForm.jiazhangxueshengEmail"  placeholder='联系邮箱' clearable></el-input>
           </el-form-item>
         </el-col>

         <el-col :span="12">
             <el-form-item v-if="flag=='jiazhangxuesheng'"  label='现有余额' prop="newMoney">
                 <el-input v-model="ruleForm.newMoney"  placeholder='现有余额' disabled style="width: 100px"></el-input>
                 <a id="btn-recharge" @click="chongzhi" href="javascript:void(0)">点我充值</a>
             </el-form-item>
         </el-col>
         <el-form-item v-if="flag=='users'" label="用户名" prop="username">
             <el-input v-model="ruleForm.username"
                       placeholder="用户名"></el-input>
         </el-form-item>
         <el-col :span="12">
             <el-form-item v-if="flag!='users'"  label="性别" prop="sexTypes">
                 <el-select v-model="ruleForm.sexTypes" placeholder="请选择性别">
                     <el-option
                             v-for="(item,index) in sexTypesOptions"
                             v-bind:key="item.codeIndex"
                             :label="item.indexName"
                             :value="item.codeIndex">
                     </el-option>
                 </el-select>
             </el-form-item>
         </el-col>
         <el-col :span="24">
             <el-form-item>
                 <el-button type="primary" @click="onUpdateHandler">修 改</el-button>
             </el-form-item>
         </el-col>
     </el-row>
    </el-form>
  </div>
</template>
<script>
// 数字，邮件，手机，url，身份证校验
import { isNumber,isIntNumer,isEmail,isMobile,isPhone,isURL,checkIdCard } from "@/utils/validate";

export default {
  data() {
    return {
        ruleForm: {},
        flag: '',
        usersFlag: false,
        // sexTypesOptions : [],
// 注册表 教师
    // 注册的类型字段 教师
        // 性别
        sexTypesOptions : [],
        // 科目
        kemuTypesOptions : [],
// 注册表 家长学生
    // 注册的类型字段 家长学生
        // 学生性别
        sexTypesOptions : [],
        // 班级
        banjiTypesOptions : [],
    };
  },
  mounted() {
    //获取当前登录用户的信息
    var table = this.$storage.get("sessionTable");
    this.sessionTable = this.$storage.get("sessionTable");
    this.role = this.$storage.get("role");
    if (this.role != "管理员"){
    }

    this.flag = table;
    this.$http({
      url: `${this.$storage.get("sessionTable")}/session`,
      method: "get"
    }).then(({ data }) => {
      if (data && data.code === 0) {
        this.ruleForm = data.data;
// 注册表 教师
// 注册表 家长学生
      } else {
        this.$message.error(data.msg);
      }
    });

// 注册表 教师 的级联表
// 注册表 家长学生 的级联表

      this.$http({
          url: `dictionary/page?page=1&limit=100&sort=&order=&dicCode=sex_types`,
          method: "get"
      }).then(({ data }) => {
          if (data && data.code === 0) {
              this.sexTypesOptions = data.data.list;
          } else {
              this.$message.error(data.msg);
          }
      });
   this.$http({
       url: `dictionary/page?page=1&limit=100&sort=&order=&dicCode=kemu_types`,
       method: "get"
   }).then(({ data }) => {
       if (data && data.code === 0) {
          this.kemuTypesOptions = data.data.list;
      } else {
          this.$message.error(data.msg);
      }
    });
   this.$http({
       url: `dictionary/page?page=1&limit=100&sort=&order=&dicCode=banji_types`,
       method: "get"
   }).then(({ data }) => {
       if (data && data.code === 0) {
          this.banjiTypesOptions = data.data.list;
      } else {
          this.$message.error(data.msg);
      }
    });
  },
  methods: {
    chongzhi() {
      this.$router.replace({ path: "/pay" });
    },
    jiaoshiPhotoUploadChange(fileUrls) {
        this.ruleForm.jiaoshiPhoto = fileUrls;
    },
    jiazhangxueshengPhotoUploadChange(fileUrls) {
        this.ruleForm.jiazhangxueshengPhoto = fileUrls;
    },


    onUpdateHandler() {
                         if((!this.ruleForm.jiaoshiName)&& 'jiaoshi'==this.flag){
                             this.$message.error('教师姓名不能为空');
                             return
                         }

                             if( 'jiaoshi' ==this.flag && this.ruleForm.jiaoshiPhone&&(!isMobile(this.ruleForm.jiaoshiPhone))){
                                 this.$message.error(`手机应输入手机格式`);
                                 return
                             }
                         if((!this.ruleForm.jiaoshiIdNumber)&& 'jiaoshi'==this.flag){
                             this.$message.error('教师身份证号不能为空');
                             return
                         }

                         if((!this.ruleForm.jiaoshiPhoto)&& 'jiaoshi'==this.flag){
                             this.$message.error('教师头像不能为空');
                             return
                         }

                         if((!this.ruleForm.kemuTypes)&& 'jiaoshi'==this.flag){
                             this.$message.error('科目不能为空');
                             return
                         }

                             if( 'jiaoshi' ==this.flag && this.ruleForm.jiaoshiEmail&&(!isEmail(this.ruleForm.jiaoshiEmail))){
                                 this.$message.error(`邮箱应输入邮箱格式`);
                                 return
                             }
                         if((!this.ruleForm.jiazhangxueshengJiazhangname)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('家长姓名不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengPhone)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('家长联系方式不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengUuidNumber)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('学号不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengXuesheng)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('学生姓名不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengIdNumber)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('学生身份证号不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengPhoto)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('学生头像不能为空');
                             return
                         }

                         if((!this.ruleForm.banjiTypes)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('班级不能为空');
                             return
                         }

                         if((!this.ruleForm.jiazhangxueshengNianji)&& 'jiazhangxuesheng'==this.flag){
                             this.$message.error('年级不能为空');
                             return
                         }

                             if( 'jiazhangxuesheng' ==this.flag && this.ruleForm.jiazhangxueshengEmail&&(!isEmail(this.ruleForm.jiazhangxueshengEmail))){
                                 this.$message.error(`邮箱应输入邮箱格式`);
                                 return
                             }
        if((!this.ruleForm.sexTypes) && this.flag!='users'){
            this.$message.error('性别不能为空');
            return
        }
      if('users'==this.flag && this.ruleForm.username.trim().length<1) {
        this.$message.error(`用户名不能为空`);
        return	
      }
      this.$http({
        url: `${this.$storage.get("sessionTable")}/update`,
        method: "post",
        data: this.ruleForm
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "修改信息成功",
            type: "success",
            duration: 1500,
            onClose: () => {
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
